import {
	promises
} from "stream"
import store from '@/store/index.js'

// 重置state数据
export function clearState(state, initData) {
	Object.keys(state).forEach(key => {
		if (!initData[key]) {
			delete state[key]
		}
	})
	Object.keys(initData).forEach(key => {
		state[key] = initData[key]
	})
}

//上传图片
export function uploadImg(event, index) {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: 'https://service.jiaoguanyi.com.cn/api/common/upload', // 图片上传
			filePath: event.file.url,
			name: 'file',
			header: {
				'token': store.state.token
			},
			success: (res) => {
				let data = JSON.parse(res.data)
				if (data.code === 200) {
					resolve(data)
				} else {
					reject(data)
				}
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

//阿里查询快递信息
export function expressCheck(oddNumber) {
	console.log(oddNumber,'11111');
	return new Promise((resolve, reject) => {
		wx.request({
			url: 'https://wuliu.market.alicloudapi.com/kdi',
			method: 'GET',
			header: {
				"Authorization": "APPCODE 26e3a36db4424b76bff7a0690a3f7721" //【3】传入自己的appcode，在买家中心查看。注意appcode与值之间有一个必须的空格 58ac025da**********3341f029ce  改为自己的APPCODE
			},
			data: {
				// type: "zto",
				no: oddNumber
			},
			success: function(res) {
				if (res.statusCode === 200) {
					resolve(res.data)
				} else {
					reject(res)
				}
			},
			fail: function(err) {
				reject(err)
			}
		})
	})
}

//时间戳转换
export function dateNew(value) {
	let date = new Date(parseInt(value));
	let y = date.getFullYear();
	let m = (date.getMonth() + 1 + '').padStart(2, '');
	let d = (date.getDate() + '').padStart(2, '0');
	let h = date.getHours();
	let minute = date.getMinutes();
	let second = date.getSeconds();
	minute = minute < 10 ? "0" + minute : minute;
	second = second < 10 ? "0" + second : second;
	return `${y}-${m}-${d} ${h}:${minute}:${second}`
}
